﻿using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.VR.Model;
using HCQ2_Model.VR.Params;
using HCQ2_Model.VRDD.InputDto._0714;
using HCQ2_Model.VRDD.OutputDto._0714;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_BLL
{
    public partial class T_VRUserLevelBLL: IT_VRUserLevelBLL
    {
        /// <summary>
        ///  获取用户等级一栏数据
        /// </summary>
        /// <param name="param">查询参数</param>
        /// <param name="total">返回总的记录条数</param>
        /// <returns></returns>
       public List<UserLevelModel> GetUserLevelData(UserlevelParam param, out int total)
        {
            List<UserLevelModel> list = new List<UserLevelModel>();
            var query= DBSession.IT_VRUserLevelDAL.GetUserLevelData(param, out total);
            query.ForEach(o => list.Add(o.ToPOCO()));
            return list;
        }
        /// <summary>
        ///  重新计算用户销售额及统计级别
        /// </summary>
        /// <param name="user_id">用户id</param>
        /// <param name="folder_id">店铺id</param>
        /// <param name="year">年</param>
        /// <returns></returns>
        public UserLevelColumn ResetUserLevel(int user_id,int folder_id, int year)
        {
            UserLevelColumn result = new UserLevelColumn();
            //1.获取下线人数
            int lineNum;
            if(folder_id>0)
                lineNum = DBSession.IT_VRFounderApplyDAL.SelectCount(s => s.ref_id == user_id && s.folder_id == folder_id && s.apply_status == 1);
            else
                lineNum = DBSession.IT_VRFounderApplyDAL.SelectCount(s => s.ref_id == user_id && s.apply_status == 1);
            //2.获取销售金额
            decimal sellNum = DBSession.IT_VRUserLevelLogDAL.getSellNum(user_id, year);
            //3.确定用户等级
            int userLevel = DBSession.IT_VRUserLevelLogDAL.getUserLevel(lineNum, sellNum, folder_id);
            result.lineNum = lineNum;
            result.saleNum = sellNum;
            result.userLevel = userLevel;
            return result;
        }
        /// <summary>
        ///  获取商品折扣
        /// </summary>
        /// <param name="user_level">用户等级</param>
        /// <param name="file_id">商品id</param>
        /// <param name="folder_id">店铺id</param>
        /// <returns></returns>
        public decimal GetGoodRate(int user_level, int file_id, int folder_id)
        {
            return DBSession.IT_VRUserLevelLogDAL.GetGoodRate(user_level, file_id, folder_id);
        }
        /// <summary>
        ///  获取积分列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<UserIntegralLogModel> GetTotalGoldList(TotalLogParam param, out int total)
        {
            return DBSession.IT_VRUserLevelDAL.GetTotalGoldList(param,out total);
        }
        /// <summary>
        ///  获取用户店铺等级成长值
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public OutputUserGrow GetUserGrowUpByStore(InputUserLevelGrowUp param)
        {
            //获取当年销售
            UserLevelGrowUp obj = DBSession.IT_VRUserLevelDAL.GetUserGrowUp(param);
            if (obj == null)
                return null;
            //获取店铺下一等级
            var store = DBSession.IT_VRUserLevelDAL.Select(s => s.store_id == param.store_id && s.user_level > obj.user_level).OrderBy(s => s.user_level).FirstOrDefault();
            decimal other_sale = 0;
            int other_line=0;
            if (store != null && store.min_sale > 0)
                other_sale = store.min_sale - obj.sale_num;
            if (store != null && store.min_line > 0)
                other_line = store.min_line - obj.line_num;
            OutputUserGrow result = new OutputUserGrow
            {
                user_level = obj.user_level,
                apply_date = obj.apply_date,
                ref_name = obj.ref_name,
                sale_num = obj.sale_num,
                line_num = obj.line_num,
                other_sale = other_sale < 0 ? 0 : other_sale,
                other_line = other_line < 0 ? 0 : other_line,
                is_high = store == null ? true : false
            };
            return result;
        }
        /// <summary>
        ///  获取店铺根节点id
        /// </summary>
        /// <param name="store_id"></param>
        /// <returns></returns>
        public int GetRootIdByStore(int store_id)
        {
            return DBSession.IT_VRUserLevelDAL.GetRootIdByStore(store_id);
        }
        /// <summary>
        ///  获取店铺根节点对象
        /// </summary>
        /// <param name="store_id"></param>
        /// <returns></returns>
        public T_DocumentFolder GetRootIdByStore1(int store_id)
        {
            return DBSession.IT_VRUserLevelDAL.GetRootIdByStore1(store_id);
        }
    }
}